<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!-- 授权panel -->
<div id="authDlg" class="easyui-dialog" style="width:460px;height:500px;padding:10px;" buttons="#authDlg-buttons" data-options="closed:true,modal:true,resizable:true">
	<div class="easyui-layout" data-options="fit:true,border:false">
		<div data-options="region:'west'" style="width:310px;padding:10px;">
			<ul id="accessTree"></ul>				
			<input id="masterId" name="masterId" hidden="hidden"/>
		</div>
		<div data-options="region:'center'" style="text-align:center;padding-top:100px;">
			<div>
				<a class="easyui-linkbutton" onclick="checkAll();" style="width:60px;">全选</a>
				<br /> <br />
				<a class="easyui-linkbutton" onclick="checkInverse();" style="width:60px;">反选</a>
				<br /> <br />
				<a class="easyui-linkbutton" onclick="uncheckAll();" style="width:60px;">取消</a>
			</div>
		</div>
	</div>
</div>
<div id="authDlg-buttons">
	<a href="javascript:void(0)" class="easyui-linkbutton c8" iconCls="icon-ok" onclick="saveAuth();" style="width:90px">保存</a>
	<a href="javascript:void(0)" class="easyui-linkbutton c2" iconCls="icon-cancel" onclick="javascript:$('#authDlg').dialog('close')" style="width:90px">取消</a>
</div>
<!-- 授权panel结束 -->


<script type="text/javascript">
	//==================================授权=============================
	var accessTree;
	function openAuth(){
		
		
		var row = $('#datagd').datagrid('getSelected');
		if (row){
			$('#authDlg').dialog('open').dialog('setTitle','授权');
			//-----------可分配权限资源树--------------
			accessTree = $('#accessTree').tree({
				url : '${pageContext.request.contextPath}/sys/access/auth_tree',
				parentField : 'parentId',
				lines : true,
				checkbox : true,
				cascadeCheck:false
			});
			$('#authForm').form('clear');
			$('#masterId').val(row.id);
			loadAuth(row.id);
		} else {
			$.messager.alert('提示','请选择一条记录');
		}
	}

	//加载该角色已有权限
	function loadAuth(id){
		$.ajax({
			url:'${pageContext.request.contextPath}/sys/auth/auth_load',
			type:'post',
			dataType:'json',
			data:{master_id:id},
			success: function(result){
				for(var i=0;i<result.length;i++){
					if (accessTree.tree('find', result[i].accessId)) {
						accessTree.tree('check', accessTree.tree('find', result[i].accessId).target);
					}
				}
			}
		});
	}

	//全选
	function checkAll() {
		var nodes = accessTree.tree('getChecked', 'unchecked');
		if (nodes && nodes.length > 0) {
			for ( var i = 0; i < nodes.length; i++) {
				accessTree.tree('check', nodes[i].target);
			}
		}
	}
	//取消选择
	function uncheckAll() {
		var nodes = accessTree.tree('getChecked');
		if (nodes && nodes.length > 0) {
			for ( var i = 0; i < nodes.length; i++) {
				accessTree.tree('uncheck', nodes[i].target);
			}
		}
	}
	//反选
	function checkInverse() {
		var unchecknodes = accessTree.tree('getChecked', 'unchecked');
		var checknodes = accessTree.tree('getChecked');
		if (unchecknodes && unchecknodes.length > 0) {
			for ( var i = 0; i < unchecknodes.length; i++) {
				accessTree.tree('check', unchecknodes[i].target);
			}
		}
		if (checknodes && checknodes.length > 0) {
			for ( var i = 0; i < checknodes.length; i++) {
				accessTree.tree('uncheck', checknodes[i].target);
			}
		}
	}
	
	//保存
	function saveAuth() {
		var checknodes = accessTree.tree('getChecked');
		var masterId = $('#masterId').val();
		var prams = [];
		if (checknodes && checknodes.length > 0) {
			for ( var i = 0; i < checknodes.length; i++) {
				var pram = new Object();
				pram.masterId = masterId;
				pram.accessId = checknodes[i].id;
				pram.accessCode = checknodes[i].attributes;
				prams.push(pram);
			}
		}
		//转换成json字符串。
		var jsonText = JSON.stringify(prams);
		$.post('${pageContext.request.contextPath}/sys/auth/save', {data:jsonText}, function(result) {
			if(result.success){
				$('#authDlg').dialog('close');
		        	$.messager.show({
	                    title: '提示',
	                    msg: result.msg
	                });
			}else{
				$.messager.show({
                    title: '提示',
                    msg: result.msg
                });
			}
		}, 'json');
	}
</script>